package com.card.admin.pojo;

import java.util.List;

import com.card.user.StringParam;
import com.google.common.collect.Lists;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.FilterOperator;
import com.xiaoao.net.db.Key;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.Query.FilterPredicate;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.StringUtil;

/**
 * @Description:
 * @author siyunlong
 * @date 2016年8月15日 下午7:00:46
 * @version V1.0
 */
public class JulebuManager {
	public static final String T_Julebu_DATA = "julebudata";
	//public static final String T_User_DATA = "userdata";
	private static final JulebuManager instance = new JulebuManager();

	public static JulebuManager getInstance() {
		return instance;
	}

	public List<Julebu> getJulebus(Admin admin) {
		if (admin.getGroup() == StringParam.Group_Admin) {
			return getJulebuById();
		}
		return getJulebuByFatherId(admin.getName());
	}
	
	public List<Julebu> getJulebuById() {
		List<Julebu> ads = Lists.newArrayList();
		List<Entity> entityList = getEntityByGroup();
		for (Entity entity : entityList) {
			ads.add(new Julebu(entity));
		}
		return ads;
	}
	//查询  俱乐部列表
	private List<Entity> getEntityByGroup() {
		Query query = new Query(T_Julebu_DATA);
		query.addFilter(new FilterPredicate(StringParam.Guanliyuanid, FilterOperator.GREATER_THAN_OR_EQUAL, 0));
		query.setLimit(10000);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		return entityList;
	}
	
	//查询代理的俱乐部
	public List<Julebu> getJulebuByFatherId(String guanliyuanid) {
		Query query = new Query(T_Julebu_DATA);
		query.addFilter(new FilterPredicate(StringParam.Guanliyuanid, FilterOperator.EQUAL, guanliyuanid));
		query.setLimit(10000);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		List<Julebu> ads = Lists.newArrayList();
		for (Entity entity : entityList) {
			ads.add(new Julebu(entity));
		}
		return ads;
	}

	public static Julebu getJulebu(String userName) {
		// 从数据库获取玩家数据
		List<Entity> entityList = getEntity(userName);
		if (entityList.size() > 0) {
			Entity entity = entityList.get(0);
			return new Julebu(entity);
		}
		return null;
	}

	public Julebu getJulebuById(String id) {
		// 从数据库获取玩家数据
		Entity entity = MongodbManager.getInstance().getEntityFromDB(new Key(T_Julebu_DATA, id));
		if (entity != null) {
			return new Julebu(entity);
		}
		return null;
	}
	

	/*public List<Julebu> getAgents() {
		return getJulebuByGroup(StringParam.Group_Agent);
	}*/

	private static List<Entity> getEntity(String userName) {
		Query query = new Query(T_Julebu_DATA);
		query.addFilter(new FilterPredicate(StringParam.UserName, FilterOperator.EQUAL, userName));
		query.setLimit(1);
		List<Entity> entityList = DBManager.getInstance().queryEntity(query);
		return entityList;
	}


	/*public boolean addJulebu(String userName, String id) {
		List<Entity> entityList = getEntity(userName);
		if (!entityList.isEmpty()) {
			return false;
		}
		Entity entity = new Entity(T_Julebu_DATA, PrimaryKey.getIntPrimaryKey("Julebu") + "");
		entity.setProperty(StringParam.UserName, userName);
		entity.setProperty(StringParam.Pwd, pwd);
		entity.setProperty(StringParam.Group, StringParam.Group_Julebu);
		DBManager.getInstance().saveOrUpdate(entity);
		return true;
	}

	public boolean addAgent(int fatherId, String userName, String pwd, String phone) {
		List<Entity> entityList = getEntity(userName);
		if (!entityList.isEmpty()) {
			return false;
		}
		// 添加新代理
		Entity entity = new Entity(T_Julebu_DATA, PrimaryKey.getIntPrimaryKey("Julebu") + "");
		entity.setProperty(StringParam.UserName, userName);
		entity.setProperty(StringParam.Pwd, pwd);
		entity.setProperty(StringParam.Phone, phone);
		entity.setProperty(StringParam.FatherId, fatherId);
		entity.setProperty(StringParam.Group, StringParam.Group_Agent);
		entity.setProperty(StringParam.RoomSettings, "4,4,1,0,1,6,gonghui");
		DBManager.getInstance().saveOrUpdate(entity);

		// 更新父代理
		// Julebu Julebu = JulebuManager.getInstance().getJulebu(userName);
		// System.out.println("更新父代理" + Julebu.getFatherId());
		//
		// Entity entity1 = new Entity(T_User_DATA,
		// PrimaryKey.getIntPrimaryKey("userdata") + "");
		// entity.setProperty(StringParam.ID, userName);
		// entity.setProperty(StringParam.FatherId, Julebu.getFatherId());
		// DBManager.getInstance().saveOrUpdate(entity1);
		return true;
	}

	public void changeState(String id) {
		Julebu Julebu = getJulebuById(id);
		if (Julebu == null) {
			return;
		}
		Entity entity = Julebu.getEntity();
		int state = Julebu.getState();
		state = state == StringParam.State_Free ? StringParam.State_Lock : StringParam.State_Free;
		entity.setProperty(StringParam.State, state);
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public void deleteAgent(String id) {
		Julebu Julebu = getJulebuById(id);
		if (Julebu == null) {
			return;
		}
		DBManager.getInstance().removeEntity(new Key(T_Julebu_DATA, id));
	}

	public void changePwd(String id, String newPwd) {
		Julebu Julebu = getJulebuById(id);
		if (Julebu == null) {
			return;
		}
		Entity entity = Julebu.getEntity();
		entity.setProperty(StringParam.Pwd, newPwd);
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public void addGold(String id, int addGold, int toId) {
		Julebu Julebu = getJulebuById(id);
		if (Julebu == null) {
			return;
		}
		Entity entity = Julebu.getEntity();
		int gold = Julebu.getGold() + addGold;
		Julebu.setGold(gold);
		entity.setProperty(StringParam.Gold, gold);
		Julebu.addCzLog(addGold, gold, toId);
		Julebu.saveDB();
	}

	// 更改gpgp
	public void changRoomSetting(String id, String roonsetting) {
		Julebu Julebu = getJulebuById(id);
		if (Julebu == null) {
			return;
		}
		Entity entity = Julebu.getEntity();
		Julebu.setRoomSettings(roonsetting);
		entity.setProperty(StringParam.RoomSettings, roonsetting);
		Julebu.saveDB();
	}

	public void addGold(Julebu Julebu, int addGold, String id) {
		Entity entity = Julebu.getEntity();
		int gold = Julebu.getGold() + addGold;
		Julebu.setGold(gold);
		entity.setProperty(StringParam.Gold, gold);
		Julebu.addCzLog(addGold, gold, Integer.parseInt(id));
		Julebu.saveDB();
	}

	public int resetPwd(String userName, String pwd, String newPwd) {
		Julebu Julebu = getJulebu(userName);
		if (Julebu == null) {
			return 1;
		}
		if (!StringUtil.equals(Julebu.getPassword(), pwd)) {
			return 2;
		}
		Entity entity = Julebu.getEntity();
		entity.setProperty(StringParam.Pwd, newPwd);
		DBManager.getInstance().saveOrUpdate(entity);
		return 0;
	}

	public static void main(String[] args) {
		JulebuManager manager = JulebuManager.getInstance();
	}*/
}
